| bitmap | vector | |
|---|---|---|
| file extensions | .jpg, .png, .gif | .eps, .pdf, .svg, .ai |
| common example | digital photo | google maps |
| consists of | million of pixels | points, lines and polygons |
| file size | large | small |
| software | Gimp (Photoshop) | Inkscape (Illustrator) |
| good for | web, printing (in high-res) | print, post-processing |
| exclusively used | digital photography | maps and graphs |
You can make vector graphics into bitmap graphics, but not the other way around
| interactive graphics | interactive web applications | |
|---|---|---|
| typical example | one-off javascript plot | Trade data explorer |
| look and feel | amazing but limited | amazing and unlimited |
| depends | online libraries, shipped with data | Needs a full R instance |
| R | htmlwidgets | shiny |
| good for | showing off & | showing off & explorative wiz |

library(FAOSTAT)
library(knitr)
library(dplyr)
library(ggplot2)
dat <- getFAOtoSYB(domainCode = "OA", elementCode = 551,itemCode = 3010) # Download rural population
dat1 <- dat$entity
dat <- getFAOtoSYB(domainCode = "OA", elementCode = 561,itemCode = 3010) # Download urban population
dat2 <- dat$entity
dat <- merge(dat1,dat2,by=c("FAOST_CODE","Year"))
dim(dat)
[1] 19854 4
kable(head(dat)) # print 5 first rows
| FAOST_CODE | Year | OA_3010_551 | OA_3010_561 |
|---|---|---|---|
| 100 | 1961 | 375928 | 82699 |
| 100 | 1962 | 382709 | 85253 |
| 100 | 1963 | 389708 | 87908 |
| 100 | 1964 | 396938 | 90670 |
| 100 | 1965 | 404412 | 93541 |
| 100 | 1966 | 412128 | 96528 |
names(dat) <- c("FAOST_CODE","Year","rural_pop","urban_pop")
dat$total_pop <- dat$rural_pop + dat$urban_pop # Compute total population
dat$rural_share <- round(dat$rural_pop / dat$total_pop * 100,1) # Compute rural population share of total population
library(countrycode) # Add the country name & continent using countrycode-package
dat$FAOST_CODE[dat$FAOST_CODE == 41] <- 351
dat$cntry <- countrycode(dat$FAOST_CODE, origin = "fao", destination = "country.name")
dat$cont <- countrycode(dat$FAOST_CODE, origin = "fao", destination = "continent")
dat <- na.omit(dat)
dim(dat)
[1] 16475 8
kable(head(dat)) # print 5 first rows
| FAOST_CODE | Year | rural_pop | urban_pop | total_pop | rural_share | cntry | cont |
|---|---|---|---|---|---|---|---|
| 100 | 1961 | 375928 | 82699 | 458627 | 82.0 | India | Asia |
| 100 | 1962 | 382709 | 85253 | 467962 | 81.8 | India | Asia |
| 100 | 1963 | 389708 | 87908 | 477616 | 81.6 | India | Asia |
| 100 | 1964 | 396938 | 90670 | 487608 | 81.4 | India | Asia |
| 100 | 1965 | 404412 | 93541 | 497953 | 81.2 | India | Asia |
| 100 | 1966 | 412128 | 96528 | 508656 | 81.0 | India | Asia |
library(ggplot2)
library(dplyr)
p <- ggplot(data=dat,
aes(x=rural_pop,y=urban_pop))
p + geom_point()
pie_data <- dat %>% filter(Year == 2015) %>%
group_by(cont) %>% summarise(share = sum(rural_pop))
pie_data <- pie_data %>% mutate(sum = sum(share))
p <- ggplot(pie_data, aes(x=sum/2, y = share, fill =cont, width = sum))
p <- p + geom_bar(position="fill", stat="identity")
p + coord_polar("y")
plot_data <- dat[dat$Year == 2015,]
p <- ggplot(data=plot_data,
aes(x=cntry,y=rural_share))
p + geom_bar(stat="identity")
p <- ggplot(data=dat,
aes(x=Year,y=rural_share,group=cntry))
p + geom_point() + geom_line()
plot_data <- dat %>% filter(Year == 2015)
p <- ggplot(data=plot_data,
aes(x=cntry,y=rural_share))
p + geom_bar(stat="identity")
plot_data$cntry <- factor(plot_data$cntry, levels=arrange(plot_data, -rural_share)$cntry)
p <- ggplot(data=plot_data,
aes(x=cntry,y=rural_share))
p + geom_bar(stat="identity")
plot_data <- arrange(plot_data, -rural_share)[1:20,] # top 20
p <- ggplot(data=plot_data, aes(x=cntry,y=rural_share,fill=cont))
p + geom_bar(stat="identity")
p <- ggplot(data=plot_data, aes(x=cntry,y=rural_share,fill=cont))
p <- p + geom_bar(stat="identity")
p + coord_flip()
p <- ggplot(data=plot_data, aes(x=cntry,y=rural_share,fill=cont))
p <- p + geom_bar(stat="identity")
p <- p + coord_flip()
p <- p + scale_fill_manual(values=c("#1b9e77","#d95f02","#7570b3","#e7298a","#66a61e"))
p <- p + theme_minimal()
p <- p + theme(text = element_text(family = "Open Sans"),
title = element_text(size = 20),
axis.title = element_text(size=14),
legend.position = "top")
p <- p + labs(title="Share of rural population", x=NULL,y="Share of rural population (%)")
p <- p + guides(fill = guide_legend(title = "Continent", title.position = "left", title.hjust=.5))
p
library(gisfao)
library(scales)
library(tidyr)
library(stringr)
shape <- fao_world
FAOST_CODE <- as.character(shape$FAOST_CODE)
df.d <- data.frame(FAOST_CODE, VarX = rep(NA, nrow(shape)))
dat$FAOST_CODE[dat$FAOST_CODE == 351] <- 41
mapdata <- merge(dat %>% filter(Year %in% c(1965,1980,2000,2015)), df.d, by.x = "FAOST_CODE", all.y = TRUE)
mapdata <- mapdata[c("FAOST_CODE","Year","rural_share")]
mapdata$Year <- paste0("X",mapdata$Year)
mapdata <- spread(mapdata, Year, rural_share)
mapdata$XNA <- NULL
row.names(mapdata) <- mapdata$FAOST_CODE
row.names(shape) <- as.character(shape$FAO_CODE)
mapdata <- mapdata[order(row.names(mapdata)), ]
shape <- shape[order(row.names(shape)), ]
shape2 <- maptools::spCbind(shape, mapdata)
shape2$id <- rownames(shape2@data)
map.points <- fortify(shape2, region = "id")
map.df <- merge(map.points, shape2, by = "id")
map.df <- gather(map.df, "Year", "rural_share", 28:31)
map.df$Year <- str_replace_all(map.df$Year, "X", "Year ")
map <- ggplot(data=map.df, aes(long,lat,group=group))
map <- map + geom_polygon(aes(fill = rural_share),colour=alpha("white", 3/4),size=.2)
map <- map + theme(legend.position = "top",
legend.background=element_rect(colour=NA, fill=NA),
axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank())
map <- map + facet_wrap(~Year, ncol = 2)
map
library(gisfao)
library(scales)
library(tidyr)
library(stringr)
library(RColorBrewer)
library(gridExtra)
shape <- fao_world
FAOST_CODE <- as.character(shape$FAOST_CODE)
df.d <- data.frame(FAOST_CODE, VarX = rep(NA, nrow(shape)))
dat$FAOST_CODE[dat$FAOST_CODE == 351] <- 41
mapdata <- merge(dat, df.d, by.x = "FAOST_CODE", all.y = TRUE)
mapdata <- mapdata[c("FAOST_CODE","Year","rural_share")]
mapdata$Year <- paste0("Year ",mapdata$Year)
mapdata <- spread(mapdata, Year, rural_share)
mapdata$XNA <- NULL
row.names(mapdata) <- mapdata$FAOST_CODE
row.names(shape) <- as.character(shape$FAO_CODE)
mapdata <- mapdata[order(row.names(mapdata)), ]
shape <- shape[order(row.names(shape)), ]
shape2 <- maptools::spCbind(shape, mapdata)
shape2$id <- rownames(shape2@data)
map.points <- fortify(shape2, region = "id")
map.df <- merge(map.points, shape2, by = "id")
map.df <- gather(map.df, "Year", "rural_share", 28:118)
map.df$Year <- as.character(map.df$Year)
map.df$Year <- str_replace(map.df$Year, "Year.", "")
map.df$Year <- factor(map.df$Year)
map.df$Year <- as.numeric(levels(map.df$Year))[map.df$Year]
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
y_dat <- data.frame()
for(y in unique(map.df$Year)[!is.na(unique(map.df$Year))]) {
map.df_subset <- map.df[map.df$Year == y,]
map <- ggplot(data=map.df_subset, aes(long,lat,group=group))
map <- map + geom_polygon(aes(fill = rural_share),colour=alpha("white", 3/4),size=.2)
map <- map + theme(legend.position = "top",
legend.background=element_rect(colour=NA, fill=NA),
axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank())
map <- map + labs(title=paste("Year",y))
map <- map + scale_fill_gradientn(limits = c(0,100),colours= myPalette(100))
# plot
line_data <- map.df_subset[!duplicated(map.df_subset[c("FAO_CODE")]),]
line_data <- line_data[c("FAO_CODE","rural_share","ADM0_NAME","Year")]
y_dat <- rbind(y_dat,line_data)
plot <- ggplot(y_dat, aes(x=Year,y=rural_share,group=FAO_CODE,color=rural_share)) +
geom_line() +
coord_cartesian(xlim=c(1960,2050)) +
scale_color_gradientn(limits = c(0,100),colours= myPalette(100)) +
coord_cartesian(ylim=c(0,100))
png(file=paste0("map/Year.",y,".png"), width=1000, height=700, res=120)
grid.arrange(arrangeGrob(map,plot, widths=c(3/5,2/5), ncol=2))
dev.off()
}
system("convert -delay 40 -loop 0 map/*.png map/maps.gif")